<template>
<div>
    <van-nav-bar
    :title="`${$route.query.key} 的搜索结果`"
    left-arrow
    @click-left="$router.back()"
    fixed
    placeholder
  />
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
  >
    <van-cell v-for="item in list" @click="$router.push(`/article/${item.art_id}`)" :key="item.art_id" :title="item.title" />
  </van-list>
</div>
</template>

<script>
import { resultAPI } from '@/api/search'

export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      // 记录页码数
      page: 1
    }
  },
  methods: {
    async onLoad () {
      const res = await resultAPI({
        page: this.page,
        per_page: 10,
        q: this.$route.query.key
      })
      this.page++
      this.list.push(...res.data.results)
      this.loading = false
      if (this.list.length >= res.data.total_count) {
        this.finished = true
      }
    }
  }
}
</script>

<style>

</style>
